<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\BasicException;
use App\Response\Response;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;                //引用http请求类

class CrossRoomPkController extends BaseController
{
    /**
     * @api {get} /cross_room_pk/list  跨房PK列表
     * @apiVersion 0.1.0
     * @apiName 跨房PK列表
     * @apiGroup 跨房PK
     * @apiParam {number} win_flag  PK结果，非必传
     * @apiParam {number} perPage  每页数，非必传
     * @apiParam {number} page  页码，非必传
     * @apiParam {date} start_at  PK结束时间段开始，非必传
     * @apiParam {date} end_at  PK结束时间段结束，非必传
     * @apiParam {mix} search  发起PK房间名/ID，非必传
     * @apiParam {mix} meet_search  受邀房间名/ID，非必传
     */
    public function crossRoomPkList(Request $request)
    {
        $param = $request->only(['search', 'win_flag', 'meet_search', 'page_size', 'page', 'start_at', 'end_at']);
        //参数校验
        $v = app('validator')->make(
            $param,
            [
                'win_flag' => "filled|numeric|in:0,1,2,3,4",       //PK结果,0:初始状态，1:左方胜(发起方)，2:右方胜（接收房），3:平局 4:异常
                'page_size' => 'filled|numeric',
                'page' => 'filled|numeric',
                'start_at' => 'date',
                'end_at' => 'date',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);

        $page = $param['page'] ?? 1;
        $perPage = $param['page_size'] ?? 10;
        $param['start_at'] = $param['start_at'] ?? '';
        $param['end_at'] = $param['end_at'] ?? '';

        $list = app('repo_cross_room_pk')->list($param, $page, $perPage);

        return Response::sendList($list);
    }

    /**
     * @api {get} /cross_room_pk/info  跨房PK详情
     * @apiVersion 0.1.0
     * @apiName 跨房PK列表
     * @apiGroup 跨房PK
     * @apiParam {number} id  主键ID，必传
     * */
    public function info(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->only(['id']),
            [
                'id' => "required|numeric|exists:mysql.ln_cross_room_pk,id",
            ]
        );
        if ($v->fails()) throw new BasicException(10001);

        $id = $request->input('id');

        $info = app('repo_cross_room_pk')->info($id);
        return Response::sendData($info);
    }
}
